package com.dsteiner.widgets.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IButton;
import com.dsteiner.widgets.client.Widgets.IFormPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.event.MessageEvent;
import com.dsteiner.widgets.client.model.FileUploadModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;

public class FileUploadController {

	private FileUploadModel fileUploadModel;
	private IFormPanel formPanel;
	private IHTML titleHtml;
	private IHTML maxFileSizeHtml;

	
	public void registerFileUploadWidget(ILazyWidget fileUploadWidget) {
		fileUploadWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				fileUploadModel.setSubmitResult(null);
				fileUploadModel.setSubmitted(false);
				updateActionUrl();
				updateTitle();
				updateMaxFileSizeHtml();
			}
		});
	}

	public void registerFileUploadModel(FileUploadModel fileUploadModel) {
		this.fileUploadModel = fileUploadModel;
		fileUploadModel.addPropertyChangeListener("actionUrl", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateActionUrl();
			}
		});
		fileUploadModel.addPropertyChangeListener("title", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTitle();
			}
		});
		fileUploadModel.addPropertyChangeListener("maxFileSize", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateMaxFileSizeHtml();
			}
		});
	}
	
	private void updateMaxFileSizeHtml() {
		Integer maxFileSize = fileUploadModel.getMaxFileSize();
		maxFileSizeHtml.setVisible(maxFileSize != null);
		if(maxFileSize != null)
			maxFileSizeHtml.setHTML("archivo con maximal "+Math.round(maxFileSize/1000)+" Kb");
	}

	private void updateTitle() {
		String title = fileUploadModel.getTitle();
		titleHtml.setVisible(title != null);
		if(title != null)
			titleHtml.setHTML(title);
	}

	private void updateActionUrl() {
		String actionUrl = fileUploadModel.getActionUrl();
		if(actionUrl != null)
			formPanel.setAction(actionUrl);
	}

	public void registerUploadFotoButton(IButton uploadFotoButton) {
		uploadFotoButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(!fileUploadModel.isSubmitted())
					formPanel.submit();
			}
		});
	}
	
	public void registerFormPanel(IFormPanel formPanel) {
		this.formPanel = formPanel;
		formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler(){
			public void onSubmitComplete(SubmitCompleteEvent event) {
				String results = event.getResults();
				String msg = results == null ? "" : 
					results.replace("<pre style=\"word-wrap: break-word; white-space: pre-wrap;\">", "")
					.replace("</pre>", "");
				if(msg.indexOf("Error") != -1)
					EventManagerImpl.getInstance().fireEvent(new MessageEvent(msg));
				else
					EventManagerImpl.getInstance().fireEvent(new MessageEvent("enviado exitosamente", MessageEvent.TYPE_INFO));
				fileUploadModel.setSubmitResult(msg);
				fileUploadModel.setSubmitted(false);
			}
		});
		formPanel.addSubmitHandler(new SubmitHandler(){
			public void onSubmit(SubmitEvent event) {
				fileUploadModel.setSubmitted(true);
				EventManagerImpl.getInstance().fireEvent(new MessageEvent("enviando... (por archivos grandes eso puede tardar un poco)", MessageEvent.TYPE_ACTIVITY));
			}
		});
	}

	public void registerTitleHtml(IHTML titleHtml) {
		this.titleHtml = titleHtml;
	}

	public void registerMaxFileSizeHtml(IHTML maxFileSizeHtml) {
		this.maxFileSizeHtml = maxFileSizeHtml;
	}

	
}